// pages/user-course/user-course-detail/course-appoint/course-coach/course-coach.js
const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

const currentDate = Date.parse(new Date());

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    show: {
      top: false,
    }
  },

  toggle(type) {
    this.setData({
      [`show.${type}`]: !this.data.show[type]
    });
  },

  toggleDatePopup() {
    var type = 'date';
    this.setData({
      [`show.${type}`]: !this.data.show[type],
      [`show.top`]: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid),
      FU_XING: constant.resource.FU_XING,
      coachId: options.coachId,
      siteId: options.siteId
    })

    if(options.day) {
      this.setData({
        day: options.day
      })
    }

    if(options.scheduleId) {
      this.setData({
        scheduleId: options.scheduleId
      })
    }
    
    this.data.courseRoundId = options.courseRoundId;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/available-days?unionid=' + that.data.unionid + '&scheduleId=' + (that.data.scheduleId ? that.data.scheduleId : ''),
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            days: res.data.data.availableDays
          });
        } else {
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });

    that.getAvailableTimes();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  getAvailableTimes: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/available-times-by-coach?unionid=' + that.data.unionid + '&siteId=' + that.data.siteId + '&coachId=' + that.data.coachId + (that.data.day ? ('&day=' + that.data.day) : '') + (that.data.scheduleId ? ('&scheduleId=' + that.data.scheduleId) : ''),
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          wx.setNavigationBarTitle({
            title: '教练-' + res.data.data.coachName
          });

          that.setData({
            siteName: res.data.data.siteName,
            coachName: res.data.data.coachName,
            remains: res.data.data.remainList,
            roomLimit: res.data.data.roomLimit,
            commonLimit: res.data.data.commonLimit,
            day: res.data.data.day,
            give: res.data.data.give
          });

          if (that.data.roomLimit <= 0 && that.data.commonLimit > 0) {
            that.setData({
              roomType: 'COMMON'
            })
          } else if (that.data.roomLimit > 0 && that.data.commonLimit <= 0) {
            that.setData({
              roomType: 'ROOM'
            })
          }
        } else {
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  },

  selectDate: function (e) {
    var that = this;
    this.setData({
      day: that.data.chooseDay 
    })

    that.getAvailableTimes();
    var type = 'date'
    this.setData({
      [`show.${type}`]: !this.data.show[type],
      [`show.top`]: false
    });
  },

  formSubmit: function (e) {
    var that = this;
    that.setData({
      disabled: true,
      occurHour: e.detail.target.dataset.hour
    })
    that.onSubmit()
  },

  onSubmit: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/appoint?unionid=' + that.data.unionid + '&scheduleId=' + (that.data.scheduleId ? that.data.scheduleId : ''),
      method: 'POST',
      data: {
        site_id: that.data.siteId,
        coach_id: that.data.coachId,
        occur_date: that.data.day,
        occur_hour: that.data.occurHour
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == "0") {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 1000,
            mask: true,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 3
                });
              }, 1000)
            }
          });
        } else {
          that.setData({
            disabled: false
          })
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        that.setData({
          disabled: false
        })
        wx.showToast({
          title: '预约失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  },
  chooseDate: function (e) {
    var that = this;
    this.setData({
      chooseDay: e.currentTarget.dataset.date
    })
  }
})